package com.wits.platform.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.springside.modules.utils.Collections3;

import com.wits.platform.common.constants.SysTabConstants;
import com.wits.qso.common.enumerate.MemberStatus;


@Entity
@Table(name = "wits_user")
public class User extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7985099525862951943L;

	public User() {

	}

	public User(String userId, String userPassword) {
		this.userId = userId;
		this.userPassword = userPassword;
	}

	/**
	 * 用户账号
	 */
	private String userId;

	/**
	 * 用户密码
	 */
	private String userPassword;

	/**
	 * 中文姓名
	 */
	private String userNameC;

	/**
	 * 英文姓名
	 */
	private String userNameE;

	/**
	 * 邮箱
	 */
	private String userEmail;

	/**
	 * 允许登入
	 */
	private String isEnable=SysTabConstants.SYSTAB_D_TNF_F;

	/**
	 * 账号过期日期
	 */
	private Date dateExpire;

	/**
	 * 最后登陆ip
	 */
	private String ipLogin;

	/**
	 * 最后登陆日期
	 */
	private Date dateLogin;

	private List<Role> roles = new ArrayList<Role>();

	private List<String> roleIds;

	private String checked;
	
	/**
	 * 用户类型：学生为stu,教师为ter
	 */
	private String usertype;
	


	@Id
	@Column(name = "user_id", nullable = false, length = 36)
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@Column(name = "user_password", length = 150)
	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	@Column(name = "user_name_c", length = 50)
	public String getUserNameC() {
		return userNameC;
	}

	public void setUserNameC(String userNameC) {
		this.userNameC = userNameC;
	}

	@Column(name = "user_name_e", length = 50)
	public String getUserNameE() {
		return userNameE;
	}

	public void setUserNameE(String userNameE) {
		this.userNameE = userNameE;
	}

	@Column(name = "user_email", length = 100)
	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	@Column(name = "is_enable", length = 1)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "date_expire")
	public Date getDateExpire() {
		return dateExpire;
	}

	public void setDateExpire(Date dateExpire) {
		this.dateExpire = dateExpire;
	}

	@Column(name = "ip_login", length = 30)
	public String getIpLogin() {
		return ipLogin;
	}

	public void setIpLogin(String ipLogin) {
		this.ipLogin = ipLogin;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "date_login")
	public Date getDateLogin() {
		return dateLogin;
	}

	public void setDateLogin(Date dateLogin) {
		this.dateLogin = dateLogin;
	}

	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "wits_mmr_user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	/**
	 * 用户拥有的角色名称字符串,用','分隔.
	 */
	@Transient
	public List<String> getRoleNames() {
		return Collections3.extractToList(roles, "roleName");
	}

	/**
	 * 用户拥有的角色id字符串,用','分隔.
	 */
	@Transient
	public List<String> getRoleIds() {
		if (roleIds == null) {
			roleIds = Collections3.extractToList(roles, "roleId");
		}
		return roleIds;
	}

	public void setRoleIds(List<String> roleIds) {
		this.roleIds = roleIds;
	}

	@Transient
	public String getChecked() {
		return checked;
	}

	public void setChecked(String checked) {
		this.checked = checked;
	}

	
	
	@Column(name = "user_type", length = 10)
	public String getUsertype() {
		return usertype;
	}

	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}

	private static JsonConfig jsonConfig = new JsonConfig();

	@Transient
	protected JsonConfig getJsonConfig() {
		return jsonConfig;
	}

	static {
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object arg0, String name, Object arg2) {
				if (name.equals("roles") || name.equals("employee")
						|| name.equals("jsonConfig") || name.equals("roleIds")
						|| name.equals("roleNames")
						|| name.equals("userPassword")) {
					return true;
				}
				return false;
			}
		});
	}

	@Override
	public String toString() {
		return "{userId:\"" + this.userId + "\",userNameC:\"" + this.userNameC
				+ "\",userNameE:\"" + this.userNameE + "\",isEnable:\""
				+ this.isEnable + "\",dateExpire:\"" + this.dateExpire
//				+"\",lineStatus:\""+this.onlineStatus
				+ "\",dateLogin:\"" + this.dateLogin + "\",userType:\""+this.usertype + "\"}";
		// return JSONObject.fromObject(this,jsonConfig).toString();
	}

	@Override
	public String display() {
		return this.userId;
	}

}
